/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.kcrx.autoweightapp.fragment.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import cn.kcrx.autoweightapp.R
import cn.kcrx.autoweightapp.data.NewCashBoxRecord
import cn.kcrx.autoweightapp.databinding.AdapterItemCashboxrecordBinding
import cn.kcrx.autoweightapp.models.NewCashBox
import cn.kcrx.autoweightapp.utils.slideIn
import com.bumptech.glide.Glide
import java.text.SimpleDateFormat

class CashWeightRecordAdapter:ListAdapter<NewCashBoxRecord, CashWeightRecordAdapter.NewCashBoxRecordViewHolder>(NewCashBoxRecordCallBack()) {
    inner class NewCashBoxRecordViewHolder(val binding: AdapterItemCashboxrecordBinding):RecyclerView.ViewHolder(binding.root)
    {
        fun bind(newItem: NewCashBoxRecord, i:Int){
            val context=binding.root.context
          when(newItem.cashName){
              "100元"-> Glide.with(context).load(R.drawable.yuan100).into(binding.ivCashpic)
              "50元"-> Glide.with(context).load(R.drawable.yuan50).into(binding.ivCashpic)
              "20元"-> Glide.with(context).load(R.drawable.yuan20).into(binding.ivCashpic)
              "10元"-> Glide.with(context).load(R.drawable.yuan10).into(binding.ivCashpic)
              "5元"-> Glide.with(context).load(R.drawable.yuan5).into(binding.ivCashpic)
              "1元"-> Glide.with(context).load(R.drawable.yuan1).into(binding.ivCashpic)
          }
            binding.root.slideIn()
            binding.tvCashname.text=newItem.cashName
            binding.tvCashrecordWeight.text=newItem.weight.toString()
            binding.tvCashrecordDate.text= SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newItem.datatime.time)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewCashBoxRecordViewHolder {
      return NewCashBoxRecordViewHolder(AdapterItemCashboxrecordBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun onBindViewHolder(holder: NewCashBoxRecordViewHolder, position: Int) {
       val item=getItem(position)
        holder.bind(item,position)
    }

}

class NewCashBoxRecordCallBack: DiffUtil.ItemCallback<NewCashBoxRecord>(){
    override fun areItemsTheSame(oldItem: NewCashBoxRecord, newItem: NewCashBoxRecord): Boolean {
    return oldItem.id==newItem.id
    }

    override fun areContentsTheSame(oldItem: NewCashBoxRecord, newItem: NewCashBoxRecord): Boolean {
       return oldItem==newItem
    }

}
